using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Common;
using RMAS.DAL;

namespace RMAS.DAL
{
    /// <summary>
    /// Summary description for DM_SHIFTS.
    /// </summary>
    public class DM_SHIFTSDao
    {
        private string _SQLSelectByKey;
        private string _SQLUpdate;
        private string _SQLInsert;
        private string _SQLDelete;
        private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion

        #region Constructor

        public DM_SHIFTSDao()
        {
            _dbConnection = StaticVar.ServerConnection;
            InitSqlQuery();
        }

        public DM_SHIFTSDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
        {
            string query;

            query = @"SELECT ID, IDPOS, UserName, TotalMoneyBegin, TotalMoneyEnd, TotalMoneyInShift, TotalMoneyDiscount, TotalMoneyPromotion, TotalMoneyCustomer, TotalMoneyGiveBack, DateTimeBegin, DateTimeEnd, IsBegin, IsEnd, IsFirst, TourGuide FROM DM_SHIFTS WHERE (ID = @ID)";
            _SQLSelectByKey = query;

            query = @"UPDATE DM_SHIFTS SET IDPOS = @IDPOS, UserName = @UserName, TotalMoneyBegin = @TotalMoneyBegin, TotalMoneyEnd = @TotalMoneyEnd, TotalMoneyInShift = @TotalMoneyInShift, TotalMoneyDiscount = @TotalMoneyDiscount, TotalMoneyPromotion = @TotalMoneyPromotion, TotalMoneyCustomer = @TotalMoneyCustomer, TotalMoneyGiveBack = @TotalMoneyGiveBack, DateTimeBegin = @DateTimeBegin, DateTimeEnd = @DateTimeEnd, IsBegin = @IsBegin, IsEnd = @IsEnd, IsFirst = @IsFirst, TourGuide = @TourGuide WHERE (ID = @ID)";
            _SQLUpdate = query;

            query = @"INSERT INTO DM_SHIFTS VALUES (@ID, @IDPOS, @UserName, @TotalMoneyBegin, @TotalMoneyEnd, @TotalMoneyInShift, @TotalMoneyDiscount, @TotalMoneyPromotion, @TotalMoneyCustomer, @TotalMoneyGiveBack, @DateTimeBegin, @DateTimeEnd, @IsBegin, @IsEnd, @IsFirst, @TourGuide)";
            _SQLInsert = query;

            query = @"DELETE FROM DM_SHIFTS WHERE (ID = @ID)";
            _SQLDelete = query;

            query = @"SELECT ID, IDPOS, UserName, TotalMoneyBegin, TotalMoneyEnd, TotalMoneyInShift, TotalMoneyDiscount, TotalMoneyPromotion, TotalMoneyCustomer, TotalMoneyGiveBack, DateTimeBegin, DateTimeEnd, IsBegin, IsEnd, IsFirst, TourGuide FROM DM_SHIFTS";
            _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

                if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(DM_SHIFTS obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.String);
                DalTools.addDbDataParameter(command, "IDPOS", obj.IDPOS, DbType.String);
                DalTools.addDbDataParameter(command, "UserName", obj.UserName, DbType.String);
                DalTools.addDbDataParameter(command, "TotalMoneyBegin", obj.TotalMoneyBegin, DbType.Decimal);
                DalTools.addDbDataParameter(command, "TotalMoneyEnd", obj.TotalMoneyEnd, DbType.Decimal);
                DalTools.addDbDataParameter(command, "TotalMoneyInShift", obj.TotalMoneyInShift, DbType.Decimal);
                DalTools.addDbDataParameter(command, "TotalMoneyDiscount", obj.TotalMoneyDiscount, DbType.Decimal);
                DalTools.addDbDataParameter(command, "TotalMoneyPromotion", obj.TotalMoneyPromotion, DbType.Decimal);
                DalTools.addDbDataParameter(command, "TotalMoneyCustomer", obj.TotalMoneyCustomer, DbType.Decimal);
                DalTools.addDbDataParameter(command, "TotalMoneyGiveBack", obj.TotalMoneyGiveBack, DbType.Decimal);
                DalTools.addDbDataParameter(command, "DateTimeBegin", obj.DateTimeBegin, DbType.DateTime);
                DalTools.addDbDataParameter(command, "DateTimeEnd", obj.DateTimeEnd, DbType.DateTime);
                DalTools.addDbDataParameter(command, "IsBegin", obj.IsBegin, DbType.Boolean);
                DalTools.addDbDataParameter(command, "IsEnd", obj.IsEnd, DbType.Boolean);
                DalTools.addDbDataParameter(command, "IsFirst", obj.IsFirst, DbType.Boolean);
                DalTools.addDbDataParameter(command, "TourGuide", obj.TourGuide, DbType.Boolean);

                if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(DM_SHIFTS obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.String);

                if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(DM_SHIFTS obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(DM_SHIFTS obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties

        private DM_SHIFTSList SelectListByCommand(IDbCommand command)
        {
            DM_SHIFTSList list = new DM_SHIFTSList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                DM_SHIFTS obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        obj = new DM_SHIFTS();
                        ReadProperties(obj, reader);
                        list.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private DM_SHIFTSList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            DM_SHIFTSList list = new DM_SHIFTSList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                DM_SHIFTS obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        obj = new DM_SHIFTS();
                        ReadProperties(obj, reader);
                        list.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ReadProperties(DM_SHIFTS obj, IDataReader reader)
        {
            try
            {
                if (!(reader["ID"] is DBNull))
                {
                    obj.ID = (string)reader["ID"];
                }
                if (!(reader["IDPOS"] is DBNull))
                {
                    obj.IDPOS = (string)reader["IDPOS"];
                }
                if (!(reader["UserName"] is DBNull))
                {
                    obj.UserName = (string)reader["UserName"];
                }
                if (!(reader["TotalMoneyBegin"] is DBNull))
                {
                    obj.TotalMoneyBegin = (decimal)reader["TotalMoneyBegin"];
                }
                if (!(reader["TotalMoneyEnd"] is DBNull))
                {
                    obj.TotalMoneyEnd = (decimal)reader["TotalMoneyEnd"];
                }
                if (!(reader["TotalMoneyInShift"] is DBNull))
                {
                    obj.TotalMoneyInShift = (decimal)reader["TotalMoneyInShift"];
                }
                if (!(reader["TotalMoneyDiscount"] is DBNull))
                {
                    obj.TotalMoneyDiscount = (decimal)reader["TotalMoneyDiscount"];
                }
                if (!(reader["TotalMoneyPromotion"] is DBNull))
                {
                    obj.TotalMoneyPromotion = (decimal)reader["TotalMoneyPromotion"];
                }
                if (!(reader["TotalMoneyCustomer"] is DBNull))
                {
                    obj.TotalMoneyCustomer = (decimal)reader["TotalMoneyCustomer"];
                }
                if (!(reader["TotalMoneyGiveBack"] is DBNull))
                {
                    obj.TotalMoneyGiveBack = (decimal)reader["TotalMoneyGiveBack"];
                }
                if (!(reader["DateTimeBegin"] is DBNull))
                {
                    obj.DateTimeBegin = (DateTime)reader["DateTimeBegin"];
                }
                if (!(reader["DateTimeEnd"] is DBNull))
                {
                    obj.DateTimeEnd = (DateTime)reader["DateTimeEnd"];
                }
                if (!(reader["IsBegin"] is DBNull))
                {
                    obj.IsBegin = (bool)reader["IsBegin"];
                }
                if (!(reader["IsEnd"] is DBNull))
                {
                    obj.IsEnd = (bool)reader["IsEnd"];
                }
                if (!(reader["IsFirst"] is DBNull))
                {
                    obj.IsFirst = (bool)reader["IsFirst"];
                }
                if (!(reader["TourGuide"] is DBNull))
                {
                    obj.TourGuide = (bool)reader["TourGuide"];
                }
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion

        public DM_SHIFTS SelectByKey(string ID)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
            DalTools.addDbDataParameter(command, "ID", ID, DbType.String);
            DM_SHIFTSList list = SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public DM_SHIFTS SelectByKey(string ID, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
            DalTools.addDbDataParameter(command, "ID", ID, DbType.String);
            DM_SHIFTSList list = SelectListByCommand(command, trans);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public DM_SHIFTSList SelectAll()
        {
            IDbCommand command = _dbConnection.CreateCommand();
            if (!StaticVar.IsServer)
            {
                _SQLSelectAll = _SQLSelectAll + " WHERE IDPOS = @IDPOS";
                DalTools.addDbDataParameter(command, "IDPOS", StaticVar.CPUID, DbType.String);
            }
            command.CommandText = _SQLSelectAll;
            DM_SHIFTSList list = SelectListByCommand(command);
            return list;
        }
        public DM_SHIFTSList SelectAll(IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            if (!StaticVar.IsServer)
            {
                _SQLSelectAll = _SQLSelectAll + " WHERE IDPOS = @IDPOS";
                DalTools.addDbDataParameter(command, "IDPOS", StaticVar.CPUID, DbType.String);
            }
            command.CommandText = _SQLSelectAll;
            DM_SHIFTSList list = SelectListByCommand(command, trans);
            return list;
        }
        public DM_SHIFTSList SelectByCommand(string Command)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = Command;
            DM_SHIFTSList list = SelectListByCommand(command);
            return list;
        }

        public DM_SHIFTSList SelectByCondition(DM_SHIFTS obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM DM_SHIFTS " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DM_SHIFTSList list = SelectListByCommand(command, trans);
            return list;
        }

        public DM_SHIFTSList SelectByCondition(DM_SHIFTS obj)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM DM_SHIFTS " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DM_SHIFTSList list = SelectListByCommand(command);
            return list;
        }
        private void DeleteByCondition(DM_SHIFTS obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM DM_SHIFTS " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(DM_SHIFTS obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            if (obj.IsSetID)
            {
                s += " AND ID = @ID ";
            }
            if (obj.IsSetIDPOS)
            {
                s += " AND IDPOS = @IDPOS ";
            }
            if (obj.IsSetUserName)
            {
                s += " AND UserName = @UserName ";
            }
            if (obj.IsSetTotalMoneyBegin)
            {
                s += " AND TotalMoneyBegin = @TotalMoneyBegin ";
            }
            if (obj.IsSetTotalMoneyEnd)
            {
                s += " AND TotalMoneyEnd = @TotalMoneyEnd ";
            }
            if (obj.IsSetTotalMoneyInShift)
            {
                s += " AND TotalMoneyInShift = @TotalMoneyInShift ";
            }
            if (obj.IsSetTotalMoneyDiscount)
            {
                s += " AND TotalMoneyDiscount = @TotalMoneyDiscount ";
            }
            if (obj.IsSetTotalMoneyPromotion)
            {
                s += " AND TotalMoneyPromotion = @TotalMoneyPromotion ";
            }
            if (obj.IsSetTotalMoneyCustomer)
            {
                s += " AND TotalMoneyCustomer = @TotalMoneyCustomer ";
            }
            if (obj.IsSetTotalMoneyGiveBack)
            {
                s += " AND TotalMoneyGiveBack = @TotalMoneyGiveBack ";
            }
            if (obj.IsSetDateTimeBegin)
            {
                s += " AND DateTimeBegin = @DateTimeBegin ";
            }
            if (obj.IsSetDateTimeEnd)
            {
                s += " AND DateTimeEnd = @DateTimeEnd ";
            }
            if (obj.IsSetIsBegin)
            {
                s += " AND IsBegin = @IsBegin ";
            }
            if (obj.IsSetIsEnd)
            {
                s += " AND IsEnd = @IsEnd ";
            }
            if (obj.IsSetIsFirst)
            {
                s += " AND IsFirst = @IsFirst ";
            }
            if (obj.IsSetTourGuide)
            {
                s += " AND TourGuide = @TourGuide ";
            }
            if (s.Length > 0)
            {
                s = " WHERE " + s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, DM_SHIFTS obj)
        {
            if (obj == null) { return; }
            if (obj.IsSetID)
            {
                DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.String);
            }
            if (obj.IsSetIDPOS)
            {
                DalTools.addDbDataParameter(command, "IDPOS", obj.IDPOS, DbType.String);
            }
            if (obj.IsSetUserName)
            {
                DalTools.addDbDataParameter(command, "UserName", obj.UserName, DbType.String);
            }
            if (obj.IsSetTotalMoneyBegin)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyBegin", obj.TotalMoneyBegin, DbType.Decimal);
            }
            if (obj.IsSetTotalMoneyEnd)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyEnd", obj.TotalMoneyEnd, DbType.Decimal);
            }
            if (obj.IsSetTotalMoneyInShift)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyInShift", obj.TotalMoneyInShift, DbType.Decimal);
            }
            if (obj.IsSetTotalMoneyDiscount)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyDiscount", obj.TotalMoneyDiscount, DbType.Decimal);
            }
            if (obj.IsSetTotalMoneyPromotion)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyPromotion", obj.TotalMoneyPromotion, DbType.Decimal);
            }
            if (obj.IsSetTotalMoneyCustomer)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyCustomer", obj.TotalMoneyCustomer, DbType.Decimal);
            }
            if (obj.IsSetTotalMoneyGiveBack)
            {
                DalTools.addDbDataParameter(command, "TotalMoneyGiveBack", obj.TotalMoneyGiveBack, DbType.Decimal);
            }
            if (obj.IsSetDateTimeBegin)
            {
                DalTools.addDbDataParameter(command, "DateTimeBegin", obj.DateTimeBegin, DbType.DateTime);
            }
            if (obj.IsSetDateTimeEnd)
            {
                DalTools.addDbDataParameter(command, "DateTimeEnd", obj.DateTimeEnd, DbType.DateTime);
            }
            if (obj.IsSetIsBegin)
            {
                DalTools.addDbDataParameter(command, "IsBegin", obj.IsBegin, DbType.Boolean);
            }
            if (obj.IsSetIsEnd)
            {
                DalTools.addDbDataParameter(command, "IsEnd", obj.IsEnd, DbType.Boolean);
            }
            if (obj.IsSetIsFirst)
            {
                DalTools.addDbDataParameter(command, "IsFirst", obj.IsFirst, DbType.Boolean);
            }
            if (obj.IsSetTourGuide)
            {
                DalTools.addDbDataParameter(command, "TourGuide", obj.TourGuide, DbType.Boolean);
            }
        }
        #endregion
    }
}